<?php

declare(strict_types=1);

namespace App\Controller\m1;

use Throwable;
use App\Helper\Log;
use App\Helper\Util;
use App\Helper\Common;
use App\Helper\Verify;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\Manager\AuthMiddleware;
use App\Service\OfficeService;

#[Controller]
#[Middleware(AuthMiddleware::class)]
class OfficeController extends AbstractController
{
    #科室列表
    #[RequestMapping(path: "list", methods: "post")]
    public function list()
    {
        $realname = $this->request->input('realname', '');
        $phone = $this->request->input('phone', '');
        $hospital_id = $this->request->input('hospital_id', 0);
        $office_type_id = $this->request->input('office_type_id', 0);
        $office = $this->request->input('office', '');
        $currentPage = $this->request->input('currentPage', 1);
        $pageSize = $this->request->input('pageSize', 15);
        $where = [['is_del', '=', 0]];
        if (!empty($realname)) {
            $cap_id = Db::table('user')
                ->where(['realname'=>$realname])
                ->value("user_id");
            $office_id = Db::table('office_member')
                ->where(['status'=>1,'is_del'=>0,'role'=>"cap",'user_id'=>$cap_id])
                ->value("office_id");
            $where[] = ['id', '=', $office_id];
        }
        if (!empty($phone)) {
            $cap_id = Db::table('user')
                ->where(['phone'=>$phone])
                ->value("user_id");
            $office_id = Db::table('office_member')
                ->where(['status'=>1,'is_del'=>0,'role'=>"cap",'user_id'=>$cap_id])
                ->value("office_id");
            $where[] = ['id', '=', $office_id];
        }
        if (!empty($hospital_id)) {
            $where[] = ['hospital_id', '=', $hospital_id];
        }
        if (!empty($office_type_id)) {
            $where[] = ['office_type', '=', $office_type_id];
        }
        if (!empty($office)) {
            $where[] = ['name', 'like', "%".trim($office)."%"];
        }
        $offset = ($currentPage - 1) * $pageSize;
        $columns = [
            'id',
            'name',
            'hospital_id',
            'hospital_name',
            'office_type',
            'office_type_name',
            'status',
            'created_at',
            'updated_at',
        ];
        $query = Db::table('office')
            ->select($columns)
            ->where($where);
        $total = $query->count();
        $datas = $query->offset($offset)
            ->limit($pageSize)
            ->orderBy('created_at', 'desc')
            ->get()
            ->toArray();
        $return_list = [];
        if(!empty($datas)){
            $roleArr = ["cap"=>'护士长', "member"=>'护士'];
            foreach ($datas as $data) {
                $cap_id = Db::table('office_member')
                    ->where(['status'=>1,'is_del'=>0,'role'=>"cap",'office_id'=>$data['id']])
                    ->value("user_id");
                $cap_info = Db::table('user')
                    ->where("id", $cap_id)
                    ->first();
                $realname = "-";
                $phone = "-";
                if(!empty($cap_info['realname'])){
                    $realname = $cap_info['realname'];
                }
                if(!empty($cap_info['phone'])){
                    $phone = $cap_info['phone'];
                }
                $member_number = Db::table('office_member')
                    ->where(['status'=>1,'is_del'=>0,'office_id'=>$data['id']])
                    ->count();
                $return_list[] = [
                    'id'=>$data['id'],
                    'realname'=>$realname,
                    'phone'=>$phone,
                    'hospital_name'=>$data['hospital_name'],
                    'office_type_name'=>$data['office_type_name'],
                    'office'=>$data['name'],
                    'member_number'=>$member_number,
                    'created_at'=>date("Y-m-d H:i:s", $data['created_at']),
                    'updated_at'=>date("Y-m-d H:i:s", $data['updated_at'])
                ];
            }
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', ['total'=>$total, 'list'=>$return_list]);
    }
    #查看人员
    #[RequestMapping(path: "members", methods: "post")]
    public function members()
    {
        $office_id = $this->request->input('office_id', 0);
        $columns = [
            'user_id',
            'role'
        ];
        $datas = Db::table('office_member')
            ->select($columns)
            ->where(['status'=>1,'is_del'=>0,'office_id'=>$office_id])
            ->get()
            ->toArray();
        $return_data = [];
        if(!empty($datas)){
            foreach ($datas as $data) {
                $user_info = Db::table('user')
                    ->select(['realname','avatar','phone'])
                    ->where("id", $data['user_id'])
                    ->first();
                $realname = !empty($user_info['realname'])?$user_info['realname']:"";
                $avatar = !empty($user_info['avatar'])?$user_info['avatar']:"";
                $phone = !empty($user_info['phone'])?$user_info['phone']:"";
                $return_data[] = [
                    'realname'=>$realname,
                    'avatar'=>$avatar,
                    'phone'=>$phone,
                    'role'=>$data['role']
                ];
            }
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $return_data);
    }
    #查看排班
    #[RequestMapping(path: "trad", methods: "post")]
    public function trad()
    {
        $office_id = $this->request->input('office_id', 0);
        $begin_date = $this->request->input("begin_date", "");
        $end_date = $this->request->input("end_date", "");
        $params = [
            'office_id'=>$office_id,
            'begin_date'=>$begin_date,
            'end_date'=>$end_date,
        ];
        $officeServ = new OfficeService();
        $officeServ->setOfficeId($office_id);
        $return_data = $officeServ->getCalendar($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $return_data);
    }
    #班种列表
    #[RequestMapping(path: "tradlist", methods: "post")]
    public function tradlist()
    {
        $realname = $this->request->input('realname', '');
        $phone = $this->request->input('phone', '');
        $tradname = $this->request->input('tradname', '');
        $currentPage = $this->request->input('currentPage', 1);
        $pageSize = $this->request->input('pageSize', 15);
        $where = [['office_trad.is_del', '=', 0],['office.is_del', '=', 0]];
        if (!empty($realname)) {
            $cap_id = Db::table('user')
                ->where(['realname'=>$realname])
                ->value("id");
            $office_id = Db::table('office_member')
                ->where(['status'=>1,'is_del'=>0,'role'=>"cap",'user_id'=>$cap_id])
                ->value("office_id");
            $where[] = ['office_id', '=', $office_id];
        }
        if (!empty($phone)) {
            $cap_id = Db::table('user')
                ->where(['phone'=>$phone])
                ->value("id");
            $office_id = Db::table('office_member')
                ->where(['status'=>1,'is_del'=>0,'role'=>"cap",'user_id'=>$cap_id])
                ->value("office_id");
            $where[] = ['office_trad.office_id', '=', $office_id];
        }
        if (!empty($tradname)) {
            $where[] = ['office_trad.name', 'like', "%".trim($tradname)."%"];
        }
        $offset = ($currentPage - 1) * $pageSize;
        $columns = [
            'office_trad.id',
            'office_trad.office_id',
            'office_trad.type',
            'office_trad.name',
            'office_trad.color',
            'office_trad.work_time',
            'office_trad.work_hours',
            'office_trad.coe',
            'office_trad.status',
            'office_trad.created_at',
            'office_trad.updated_at',
        ];
        $query = Db::table('office_trad')
            ->leftJoin('office', 'office_trad.office_id', '=', 'office.id')
            ->select($columns)
            ->where($where);
        $total = $query->count();
        $datas = $query->offset($offset)
            ->limit($pageSize)
            ->orderBy('office_trad.created_at', 'desc')
            ->get()
            ->toArray();
        $return_list = [];
        if(!empty($datas)){
            $roleArr = ["work"=>'正常上班', "rest"=>'休息'];
            foreach ($datas as $data) {
                $cap_id = Db::table('office_member')
                    ->where(['status'=>1,'is_del'=>0,'role'=>"cap",'office_id'=>$data['office_id']])
                    ->value("user_id");
                $cap_info = Db::table('user')
                    ->where("id", $cap_id)
                    ->first();
                $color_code = Db::table('conf_color')
                    ->where("id", $data['color'])
                    ->value("color");
                $realname = "-";
                $phone = "-";
                if(!empty($cap_info['realname'])){
                    $realname = $cap_info['realname'];
                }
                if(!empty($cap_info['phone'])){
                    $phone = $cap_info['phone'];
                }
                $member_number = Db::table('office_member')
                    ->where(['status'=>1,'is_del'=>0,'office_id'=>$data['id']])
                    ->count();
                $work_time_arr = [];
                if(!empty($data['work_time'])){
                    $work_time = json_decode($data['work_time'], true);
                    foreach ($work_time as $key => $value) {
                        $work_time_arr[] = $value[0]."-".$value[1];
                    }
                }
                $return_list[] = [
                    'id'=>$data['id'],
                    'realname'=>$realname,
                    'phone'=>$phone,
                    'tradname'=>$data['name'],
                    'work_time'=>implode("  ", $work_time_arr),
                    'work_hours'=>$data['work_hours'],
                    'color'=>$data['color'],
                    'color_code'=>$color_code,
                    'coe'=>$data['coe'],
                    'type'=>$roleArr[$data['type']]??"-",
                    'status'=>$data['status']==1?"启用":"停用",
                    'created_at'=>date("Y-m-d H:i:s", $data['created_at']),
                    'updated_at'=>date("Y-m-d H:i:s", $data['updated_at'])
                ];
            }
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', ['total'=>$total, 'list'=>$return_list]);
    }
}